import Vue from "vue"
import App from "./App.vue"
import Vuex from "vuex"
import router from "./router"
import store from "./store"

Vue.config.productionTip = false

Vue.use(Vuex)
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

// 2.1 导入 vue-resource
import VueResource from 'vue-resource'
// 2.2 安装 vue-resource
Vue.use(VueResource)
// 设置请求的根路径
Vue.http.options.root = 'http://localhost:8084';
// 全局设置 post 时候表单数据格式组织形式   application/x-www-form-urlencoded
Vue.http.options.emulateJSON = true;


Vue.prototype.$store = store
// console.log('sdf' + localStorage.getItem('userInfo')||'')
// Vue.http.headers.common['token'] = store.state.userInfo.token;

// 每次刚进入 网站，肯定会 调用 main.js 在刚调用的时候，先从本地存储中，把 购物车的数据读出来，放到 store 中
var userInfo = JSON.parse(localStorage.getItem('userInfo') )|| ''
store.state.userInfo=userInfo;
// Vue.http.headers = {
//     'token':store.state.userInfo.token
// }
// alert(store.state.userInfo.token)
Vue.http.headers.common['token'] = store.state.userInfo.token;
// Vue.http.headers.common['Authorization'] = 'token ' + store.state.userInfo.token;
// console.log(store.state.userInfo.token)

// 创建全局的导航守卫
router.beforeEach((to,from, next) => {
    console.log(store.state, "store.state")
    if (store.state.userInfo || to.path==="/login"){  //这里存在死循环 login也没有 userinfo
        next()
    } else {
        next({
            path: "/login"
        })
    }
})



new Vue({
    store,
    router,
    render: h => h(App)
}).$mount("#app")



